<?php
define('DEFAULT_MODULE', '');
define('DEFAULT_CONTROLLER', 'site');
define('DEFAULT_ACTION', 'index');

class System_App_Rooter extends System_App_Noyau{    
    private $_config = array(
        'module'=>''
        ,'controler'=>''
        ,'action'=>''
    );
    
    function __construct() {
        $this->register('rooter');
        $this->_setRoot();
        $this->_checkAcl();
    }
    
    private function _checkAcl(){
        if( !$this->acl->checkAcl( $this->auth->getRole(), $this->_config ) ){
            $_SESSION['login_redirect'] = $_SERVER['REQUEST_URI'];
            $_SESSION['err_msg'] = 'Cet accès est sécurisé : veuillez vous identifier.';
            $this->_reRoot('login', 'index');
        }
    }
    
    private function _reRoot( $controller, $action, $module = '' ){
        $this->_config['module'] = $module;
        $this->_config['controller'] = $controller;
        $this->_config['action'] = $action;
    }
    
    private function _setRoot(){
        $this->_config['module'] = (isset($_REQUEST['module']) && $_REQUEST['module']!='')?$_REQUEST['module']:DEFAULT_MODULE;
        $this->_config['controller'] = (isset($_REQUEST['controller']) && $_REQUEST['controller']!='')?$_REQUEST['controller']:DEFAULT_CONTROLLER;
        $this->_config['action'] = (isset($_REQUEST['action']) && $_REQUEST['action']!='')?$_REQUEST['action']:DEFAULT_ACTION;
    }
    
    public function getConfigModule(){
        return $this->_config['module'];
    }
    public function getConfigAction(){
        return $this->_config['action'];
    }
    public function getConfigController(){
        return $this->_config['controller'];
    }
    public function getControllerName(){
        switch( $this->getConfigModule() ){
            case '':
                return $this->_getStandardControllerName();
                break;
            case 'system':
                return $this->_getSystemControllerName();
                break;
            default :
                return $this->_getModuleControllerName();
                break;
        }
    }
    private function _getStandardControllerName(){
        return 'Controller_' . ucfirst( $this->getConfigController() );
    }
    private function _getModuleControllerName(){
        return 'Module_' . ucfirst( $this->getConfigModule() ) . '_Controller_' . ucfirst( $this->getConfigController() );
    }
    private function _getSystemControllerName(){
        return 'System_Module_' . ucfirst( $this->getConfigController() ). '_Controller_' . ucfirst( $this->getConfigController() );
    }
}
?>